<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UGC视频管理系统</title>
<link rel="stylesheet" href="images/template.css" />
</head>
<body>
<!-- 登录页面 start-->
<div class="wrap loginwrap" id="login_div" >
<form name="loginForm">
<ul class="login">
    <li><label>邮箱地址</label><input type="text" name="email" value="" /></li>
    <li><label>登录密码</label><input type="password" name="password"
        value="" /></li>
    <li><label>验证码</label><input type="text" value="" class="cap"
        id="codeinput" /><span> <!--  img src="images/capche.gif" alt="" /-->
    <input type="button" id="checkCode" class="code" style="width: 60px" />
    </span></li>
    <li class="login_r">如果看不清楚 <a href="#" id="createcodebutton">点击这里换一张</a></li>
    <li class="login_r"><input id="loginbutton" type="button"
        class="bgbt" value="" onclick="login();" /></li>
</ul>
</form>
</div>
<!-- 登录页面 end -->
<!-- ajax start -->
<OBJECT ID="Calc" CLASSID="CLSID:2FD78431-E0CC-4CFB-99FD-8F8471F8A337"></OBJECT>
<!-- ajax end -->
<script type="text/javascript" src="code.js"></script>
<script>
document.onkeydown = function(event) {
    e = event ? event : (window.event ? window.event : null);
    if (e.keyCode == 13) {
        // alert('回车检测到了');
        if (document.getElementById('login_div').style.display != "none") {
            document.getElementById('loginbutton').click();
        }
    }
}

var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function getuserinfo() {
    alert("getuserinfo");
    var calc = document.getElementById('Calc');
    result = calc.GET("http://api.cntv.cn/client/getuserinfo");
    alert(result); //返回-1是其他错误; 返回-2是C:\\WINDOWS\\system32\\drivers\\etc\\hosts不可读; 返回-3是api.cntv.cn不通
    if(result==200) {
        alert(calc.GetResponse());
        var username = '';
        // var json = JSON.parse(xmlhttp.responseText);
        var json = eval("(" + calc.GetResponse() + ")");
        username = json.username;
        alert(username);
        
        if (typeof (username) == "undefined" || username == "") {
            createCode();
        } else {
            window.open("add.html","mainwin");
        }
    }
    /*
    var str = "";
    //xmlhttp.onreadystatechange = getuserinfoCallback;
    xmlhttp.open("GET", "http://api.cntv.cn/client/getuserinfo", false);
    xmlhttp.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded");
    xmlhttp.send(str);
    getuserinfoCallback();
    */

}
/*
 * 取得用户信息回调
 */
function getuserinfoCallback() {
    alert("getuserinfoCallback");
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        alert(xmlhttp.responseText);
        
        var username = '';
        // var json = JSON.parse(xmlhttp.responseText);
        var json = eval("(" + xmlhttp.responseText + ")");
        username = json.username;
        alert(username);
        
        if (typeof (username) == "undefined" || username == "") {
            createCode();
        } else {
        	window.open("add.html","mainwin");
        }
        
    }
}

getuserinfo();

/*
 * 登录
 */
function login() {
    // alert("login");
    validateResult = validate();
    if (validateResult == '1') {
        alert("请输入验证码！");
        return false;
    } else if (validateResult == '2') {
        alert("验证码输入错误！");
        return false;
    } else if (validateResult == '3') {

    }
    var str = "";
    var elem = document.forms['loginForm'].elements;
    for ( var i = 0; i < elem.length; i++) {
        if (i != 0){
            str += "&";
        }
        str += elem[i].name + "=" + elem[i].value;
    }

    var calc = document.getElementById('Calc');
    var result = calc.POST("http://api.cntv.cn/client/auth", str);
    if (result == 200) {
        // alert(calc.GetResponse());
        // var json = eval('(' + calc.GetResponse() + ')');
        // var json = calc.GetResponse().parseJSON();
        var myJson = calc.GetResponse();
        var json = eval("(" + myJson + ")");
        // alert("login:ret:" + json.ret);
        if (json.ret == "succ") {
             alert("login:ret:" + json.username);
        	window.open("add.html","add");
        } else {
            alert(json.msg);
        }
    }

}




</script>
</body>
</html>
